<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <FormItem
                        type="remoteSelect"
                        label="所属协会"
                        v-model="baTable.form.items!.parent_id"
                        prop="parent_id"
                        placeholder="所属协会"
                        :input-attr="{
                            params: { isTree: true, search: [{ field: 'parent_id', val: '5', operator: 'eq' }] },
                            pk: 'cat_id',
                            field: 'name',
                            'remote-url': '/admin/category/index',
                        }"
                    />
                    <el-form-item prop="title" :label="t('Title')">
                        <el-input
                            v-model="baTable.form.items!.title"
                            type="string"
                            :placeholder="t('Please input field', { field: t('Title') })"
                        ></el-input>
                    </el-form-item>
                    <FormItem prop="content"
                              :label="t('Content')"
                              type="editor"
                              v-model="baTable.form.items!.content"
                              :input-attr="{
                            editorType: 'wang',
                        }"
                    />
                    <!--<FormItem prop="remark"
                              label="扩展内容"
                              type="editor"
                              v-model="baTable.form.items!.remark"
                              :input-attr="{
                            editorType: 'wang',
                        }"
                              v-if="baTable.form.items!.cat_id == 12"
                    />-->
                    <FormItem prop="thumb" v-if="baTable.form.items!.is_thumb === 1" :label="t('Thumb')" type="image" v-model="baTable.form.items!.thumb" />
                    <el-form-item v-if="baTable.form.items!.is_thumb === 1">
                        <div class="block-help">
                            建议尺寸：{{ baTable.form.items!.size }}
                        </div>
                    </el-form-item>
                    <FormItem prop="video" label="视频上传" type="file" v-model="baTable.form.items!.video" v-if="baTable.form.items!.cat_id === 12" />
                    <FormItem prop="video" label="报名表" type="file" v-model="baTable.form.items!.video" v-if="baTable.form.items!.cat_id === 16" />
                    <el-form-item prop="sort" :label="t('Sort')">
                      <el-input
                          v-model="baTable.form.items!.sort"
                          type="number"
                          :placeholder="t('Please input field', { field: t('Sort') })"
                      ></el-input>
                    </el-form-item>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { buildValidatorData } from '/@/utils/validate'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    title: [buildValidatorData({ name: 'required', title: t('Title') })],
    content: [buildValidatorData({ name: 'editorRequired', title: t('Content') })],
    // status: [buildValidatorData({ name: 'required', title: t('State') })],
})
</script>
